package com.hctforgreen.greenservice.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.VideoDetailsActivity;
import com.hctforgreen.greenservice.model.VideoTypeEntity;

/**
 * 视频分类列表适配器
 * 
 * @author dhb 20141015
 * 
 */
public class VideoTypesAdapter extends BaseAdapter {

	private Activity context;
	private List<VideoTypeEntity> myList;

	public VideoTypesAdapter(Activity context, List<VideoTypeEntity> myList) {
		this.context = context;
		this.myList = myList;
	}

	public int getCount() {
		return myList.size();
	}

	public Object getItem(int position) {
		return myList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_video_type_layout, null);
			holder.tvName = (TextView) convertView
					.findViewById(R.id.tv_type_name);

			holder.tvMore = (TextView) convertView.findViewById(R.id.tv_more);
			holder.gridView = (GridView) convertView
					.findViewById(R.id.grid_details);
			holder.imageLeft = (ImageView) convertView
					.findViewById(R.id.img_type_left);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final VideoTypeEntity typeEntity = myList.get(position);
		holder.tvName.setText(typeEntity.typeName);

		if (typeEntity.entities != null && typeEntity.entities.size() > 0) {
			VideoDetailAdapter detailAdapter = new VideoDetailAdapter(context,
					typeEntity.entities, holder.gridView);
			holder.gridView.setAdapter(detailAdapter);
		} else {
			VideoDetailAdapter detailAdapter = new VideoDetailAdapter(context,
					new ArrayList<VideoTypeEntity.VideoDetailEntity>(),
					holder.gridView);
			holder.gridView.setAdapter(detailAdapter);
		}
		holder.tvMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, VideoDetailsActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("typeName", typeEntity.typeName);
				intent.putExtra("typeId", typeEntity.typeId);

				context.startActivity(intent);
			}
		});

		int type = position % 3;
		switch (type) {
		case 0:
			holder.imageLeft.setImageResource(R.drawable.ic_type_hot);
			break;
		case 1:
			holder.imageLeft.setImageResource(R.drawable.ic_type_install);
			break;
		case 2:
			holder.imageLeft.setImageResource(R.drawable.ic_type_repaire);
			break;
		default:
			holder.imageLeft.setImageResource(R.drawable.ic_type_hot);
			break;
		}

		return convertView;
	}

	class ViewHolder {
		TextView tvName;// 分类名称
		TextView tvMore;// 更多
		GridView gridView;// 视频列表
		ImageView imageLeft;

	}

}